package com.itita.weibo.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4andriod.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.FansAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;


public final class ListMyFansActivity extends Activity implements IWeiboActivity{
	
	public static final String REF_FANS="REF_FANS";//更新关注
	
	public static final String REF_MORW_FANS="REF_MORW_FANS";//更新更多关注
	
	public static final String REF_ICON="REF_ICON";//更新更多关注

	public static final String REF_FANS_CANCEL_BEGIN = "REF_FANS_CANCEL_BEGIN";

	public static final String REF_FANS_RESTORE_BEGIN = "REF_FANS_RESTORE_BEGIN";

	public static final String REF_FANS_CANCEL_FINISH = "REF_FANS_CANCEL_FINISH";

	public static final String REF_FANS_RESTORE_FINISH = "REF_FANS_RESTORE_FINISH";
	
//	public static final String 
	public static Toast pdToast = null;
	
//	private View vTitle;
//	
//	private Button btnReturn;
//
//	private Button btnHome;
//	
//	private TextView tvTitle;
	
	private ListView listView;
	
	private View progress;
	
	private int nowpage4Fans = 1;
	
	private int pagesize4Fans = 8;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		this.setContentView(R.layout.list_my_fans);
		this.initControlsAndAddControlsEvent();
		MainService.mainService.addActivity(this);
		
		
	}
	
	public void initControlsAndAddControlsEvent() {
		 final View vTitle = this.findViewById(R.id.title4ListMyFans);
		 final TextView tvTitle = (TextView)vTitle.findViewById(R.id.textView);
//		  
//		 final ImageView imageViewTitle = (ImageView) vTitle.findViewById(R.id.imageViewTitle);
//	     imageViewTitle.setImageResource(R.drawable.title_fan);
		 
		 final Button btnReturn = (Button) vTitle.findViewById(R.id.title_bt_left);
		 final Button btnHome = (Button) vTitle.findViewById(R.id.title_bt_right);
       
		 this.listView = (ListView) this.findViewById(R.id.listview4ListMyFans);
		 //
         this.progress = this.findViewById(R.id.progress4ListMyFans);
        
//         btnReturn.setText(R.string.imageviewer_back);
         btnReturn.setBackgroundResource(R.drawable.title_back);
         btnReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				 finish();
			}
		});
         
		 btnHome.setBackgroundResource(R.drawable.title_home);
		 tvTitle.setText(R.string.fans);
		 btnHome.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ListMyFansActivity.this, HomeActivity.class);
					ListMyFansActivity.this.startActivity(intent);
				}
			});
	         
		 
		 this.listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(arg3==-1){   //更多
					Toast.makeText(ListMyFansActivity.this, "正在获取跟多的关注信息", Toast.LENGTH_SHORT).show();
					nowpage4Fans++;
				    final Map<String, Integer> taskParam = new HashMap();
					taskParam.put("pagesize4Fans", ListMyFansActivity.this.pagesize4Fans);
					taskParam.put("nowpage4Fans", ListMyFansActivity.this.nowpage4Fans);
					Task tsAttention=new Task(Task.TAST_GET_MORE_ATTENTION_INFO, taskParam);
					MainService.newTaskToTaskQueneHead(tsAttention);
				
				}else if(arg3==0){  //刷新
					
					Toast.makeText(ListMyFansActivity.this, "刷新", Toast.LENGTH_SHORT).show();
					HashMap taskParam = new HashMap();
					taskParam.put("pagesize4Fans", ListMyFansActivity.this.pagesize4Fans);
					taskParam.put("nowpage4Fans", ListMyFansActivity.this.nowpage4Fans);
			    	Task tsAttention=new Task(Task.TAST_GET_MORE_FANS_INFO, taskParam);
			    	MainService.newTaskToTaskQueneHead(tsAttention);
				
				}else{
					Toast.makeText(ListMyFansActivity.this, "查看给好友的信息", Toast.LENGTH_SHORT).show();
				}
		 }
		 
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		init();
	}
	@Override
	public void init() {
		Map taskParam = new HashMap();
		taskParam.put("nowpage4Fans", nowpage4Fans);
		taskParam.put("pagesize4Fans", pagesize4Fans);
		Task task = new Task(Task.TAST_GET_FANS_INFO, taskParam);
		MainService.mainService.newTaskToTaskQueneHead(task);
		
	}

	@Override
	public void refresh(Object... param) {
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals(REF_FANS)){
			try {
				ArrayList<User> friends=(ArrayList<User>)param[1];
				FansAdapter adapter=new FansAdapter(this,friends);
				this.listView.setAdapter(adapter);//更新列表
			    this.progress.setVisibility(View.GONE);
			} catch (Exception e) {
				 e.printStackTrace();
				 Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show();
			}
		    
		}else if(flag.equals(REF_MORW_FANS)){ //更多
			ArrayList<User> friends=(ArrayList<User>)param[1];
			((FansAdapter)this.listView.getAdapter()).addMoreData(friends);
		    this.progress.setVisibility(View.GONE);
		}else if(flag.equals(REF_ICON)){
			//让数据适配器getView方法重新调用
			((FansAdapter)this.listView.getAdapter()).notifyDataSetChanged();
		}
		else if(flag.equals(REF_FANS_CANCEL_BEGIN)){
			pdToast = WeiboUtil.myToastShowProceessBar(ListMyFansActivity.this, "正在取消关注");
			pdToast.show();

		}else if(flag.equals(REF_FANS_CANCEL_FINISH)){
            //msg.obj
			HashMap<?,?> map = (HashMap<?,?>)param[1];
			Button bt = (Button)map.get("button");
			bt.setText("恢复关注");
			bt.setBackgroundResource(R.drawable.attend_do);
			
			pdToast = null;// 进度条消失
			//pd.dismiss();// 进度条消失
			
		}else if(flag.equals(REF_FANS_RESTORE_BEGIN)){
			
			pdToast = WeiboUtil.myToastShowProceessBar(ListMyFansActivity.this, "正在恢复关注");
			pdToast.show();

		}else if(flag.equals(REF_FANS_RESTORE_FINISH)){
			////System.out.println("--------------------REF_ATTEND_RESTORE_FINISH---------------------");
			HashMap<?,?> map = (HashMap<?,?>)param[1];
			Button bt = (Button)map.get("button");
			bt.setBackgroundResource(R.drawable.attend_cancel);
			bt.setText("取消关注");
			pdToast = null;// 进度条消失
		}
		
	}

}
